package com.ycy.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketDemo {

  public static void main(String[] args) {

    try {

      ServerSocket serverSocket = new ServerSocket(8090);

      while (true) {

        Socket socket = serverSocket.accept();

        Thread thread = new Thread(new Runnable() {
          @Override
          public void run() {
            InputStream inputStream = null;
            try {
              inputStream = socket.getInputStream();
              InputStreamReader reader = new InputStreamReader(inputStream);
              BufferedReader bufferedReader = new BufferedReader(reader);
              System.out.println("server ...");
              while (bufferedReader.read() != -1) {
                String x = bufferedReader.readLine();
                System.out.println(x);
                if (x.endsWith("exit")) break;
              }
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        });

        thread.start();


      }


    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
